<?php
/**
 * Yotaku Shop - yshop
 * @author Creatint<creatint@163.com>
 * @copyright www.yotaku.cn 2016
 */
namespace Admin\Controller;

use Think\Page;

class GoodsController extends AdminController
{
    public function index($p = 1)
    {

        //分类数据
        $Category = D("Category");
        $categorys = $Category->getTree();
        getDepth($categorys, 0, 'child', 'depth');
        $categorys = tree_to_list($categorys, 'child');
        $this->assign('categorys', $categorys);


        $params = I('get.');
//        trace($params,'params');

        $Goods = D("Goods");
        $goodss = $Goods->getGoods($params, $page = true);
        $show = $Goods->getShow();

        $this->assign('goodss', $goodss);
        $this->assign('show', $show);
        $this->display();
    }

    public function add()
    {
        //分类数据
        $Category = D("Category");
        $categorys = $Category->getTree();
        getDepth($categorys, 0, 'child', 'depth');
        $categorys = tree_to_list($categorys, 'child');
        $this->assign('categorys', $categorys);

        $this->display();
    }

    public function edit($id)
    {

        //分类数据
        $Category = D("Category");
        $categorys = $Category->getTree();
        getDepth($categorys, 0, 'child', 'depth');
        $categorys = tree_to_list($categorys, 'child');
        $this->assign('categorys', $categorys);


        $Goods = D("Goods");

        //获取商品数据
        $info = $Goods->getDetail($id, $field = true);
        if (!$info) {
            trace($Goods->getError(), 'error');
            //$this->error($Goods->getError());
        }


        //描述内容
        $info['content'] = html_entity_decode($info['content']);
//        trace($info,'info');

        //获取图片空间
        $info['images'] = get_item_imgs($info['multimage'], 0, 'id,path');
//        trace($info['images'],'images');
        //推荐位图片

        $info['focus_images'] = get_item_imgs($info['focus'], 0, 'id,path');


        $this->assign('info', $info);

        $this->display();
    }

    public function delete()
    {
        $Goods = D("Goods");
        $id = array_unique((array)I('id', 0));
        $res = $Goods->del($id);
        if ($res) {
//            $this->success('删除成功');
            redirect($_SERVER['HTTP_REFERER']);
        } else {
            $this->error('删除失败 ' . $Goods->getError());
        }
    }

    public function update()
    {
        if (empty($_POST)) {
            exit;
        }
        $_POST['pubstart'] = strtotime($_POST['pubstart']);
        $_POST['pubend'] = strtotime($_POST['pubend']);

        // 商品数据处理钩子
        $_POST_res = hook("admin_goods_post", $_POST);
        if($_POST_res) $_POST = $_POST_res;
//        exit;

        //活动插件
        hook("activity", $_POST);


        //处理推荐位
        if (!empty($_POST) && is_array($_POST['position'])) {
            $pos_arr = $_POST['position'];
            $pos_int = 0;
            foreach ($pos_arr as $v) {
                $pos_int += $v;
            }
            $_POST['position'] = $pos_int;
        }
        //处理活动
        if (!empty($_POST) && is_array($_POST['activity'])) {
            $act_arr = $_POST['activity'];
            $act_int = 0;
            foreach ($act_arr as $v) {
                $act_int += $v;
            }
            $_POST['activity'] = $act_int;
        }
//
//        //处理颜色
//        if(!empty($_POST['color'])){
////            $_POST['color'] = json_encode($_POST['color']);
//            $_POST['color'] = '{"item":[{"id":1,"cover":12,"title":"\u8863\u54c1\u5929\u6210 2016\u79cb\u88c5\u65b0\u54c1\u7537\u88c5\u9488\u7ec7\u5f00\u886b \u97e9\u7248\u6761\u7eb9\u65f6\u5c1a\u9752\u5e74\u6bdb\u8863\u5916\u5957","price":178,"count":1},{"id":2,"cover":13,"title":"\u8863\u670d\u8863\u670d","price":100,"count":2}]}';
//        }


        //执行保存
        $Goods = D("Goods");
        $res = $Goods->updateGoods($_POST);

        if (!$res) {
            $this->error('错误提示：' . $Goods->getError());
        } else {
            $this->success($res['id'] ? '更新成功' : '新增成功');
        }
    }

    public function copy($id=null)
    {
        if(!($id>0)){
            $this->error("请选择要复制的商品");
        }

        // TODO:通过id获取商品数据
        $Goods = D("Goods");
        if(!($goods = $Goods->getDetail($id))){
            $this->error($Goods->getError());
        }

        // TODO:去掉id、position、activity、stock、sales、pubdate、created_at、update_at 新增商品
        unset($goods['id']);
        unset($goods['position']);
        unset($goods['activity']);
        unset($goods['stock']);
        unset($goods['sales']);
        unset($goods['pubdate']);
        unset($goods['created_at']);
        unset($goods['update_at']);


        // TODO:添加商品
        if($res = $Goods->updateGoods($goods)){
            $this->success("复制成功",U("Goods/edit",array("id"=>$res['id'])));
        }else{
            $this->error("复制失败",$Goods->getError());
        }

    }
}